package net.ojbk.timeAxis.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import net.ojbk.timeAxis.po.Time;
import net.ojbk.timeAxis.po.TimeAxis;
import net.ojbk.timeAxis.po.TimeAxisPage;
import net.ojbk.timeAxis.tool.DBTool;

public class TimeAxisService {
	
	public int modifyMsgPw(String axisId, String newPw) { // 5失败
		SqlSession sqlSession = DBTool.getSession();
		try {
			TimeAxis timeAxis = sqlSession.selectOne("timeAxisMap.selectOne", axisId);
			if (timeAxis != null) {
				timeAxis.setMsgPw(newPw);
				sqlSession.update("timeAxisMap.updateOne", timeAxis);
				return 1;
			} else {
				return 5;
			}
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	public boolean CheckmodifyMsgPw(String userId, String axisId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			TimeAxis timeAxis = sqlSession.selectOne("timeAxisMap.selectOne", axisId);
			if (timeAxis == null)
				return false;
			if (timeAxis.getUserId().equals(userId))
				return true;
			return false;
		} catch(Exception e) {
			return false;
		} finally {
			sqlSession.close();
		}
	}
	
	public int modifyMsgConfig(String axisId, boolean newMsgConfig) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			TimeAxis timeAxis = sqlSession.selectOne("timeAxisMap.selectOne", axisId);
			if (timeAxis != null) {
				timeAxis.setMsgConfig(newMsgConfig);;
				sqlSession.update("timeAxisMap.updateOne", timeAxis);
				return 1;
			} else {
				return 5;
			}
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	
	public int modifyBrowseConfig(String axisId, boolean newBrowseConfig) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			TimeAxis timeAxis = sqlSession.selectOne("timeAxisMap.selectOne", axisId);
			if (timeAxis != null) {
				timeAxis.setBrowseConfig(newBrowseConfig);
				sqlSession.update("timeAxisMap.updateOne", timeAxis);
				return 1;
			} else {
				return 5;
			}
		} catch(Exception e) {
			return 5;
		} finally {
			sqlSession.close();
		}
	}
	
	// JSP 数据获取部分
	public List<TimeAxis> getAllTimeAxisInfo(String userId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			List<TimeAxis> temp = sqlSession.selectList("timeAxisMap.selectAllByUserId", userId);
			System.out.println(temp);
			return temp;
		} catch(Exception e) {
			return null;
		} finally {
			sqlSession.close();
		}
	}
	
	public TimeAxis getTimeAxisInfo(String axisId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			TimeAxis timeAxis = sqlSession.selectOne("timeAxisMap.selectOne", "axisId");
			return timeAxis;
		} catch(Exception e) {
			return null;
		} finally {
			sqlSession.close();
		}
	}
	
	public List<TimeAxisPage> getAllTimeAxisPageInfo(String axisId) {
		SqlSession sqlSession = DBTool.getSession();
		try {
			return sqlSession.selectList("timeAxisPageMap.selectAllByAxisId", axisId);
		} catch(Exception e) {
			return null;
		} finally {
			sqlSession.close();
		}
	}
	
}
